package com.eamois.dao.sys.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.sys.DifferentMeaDao;
import com.eamois.domain.manual.Catalog;
import com.eamois.domain.manual.Technology;
import com.eamois.domain.sys.DifferentMea;

public class DifferentMeaDaoImpl extends BaseDaoImpl<DifferentMea> implements DifferentMeaDao{

	@Override
	public int findTeamCount() {
		// TODO Auto-generated method stub
		
		return (Integer)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
//				String sql = "SELECT DISTINCT(team) FROM ietm_different_mea";
//				session.createSQLQuery(sql);
				Criteria criteria = session.createCriteria(DifferentMea.class);
				criteria.setProjection(Projections.distinct(Projections.property("team")));
				Collection<String> list = criteria.list();
				if (list.size() == 0) {
					return 0;
				}else{
					return list.size();
				}
			}
		});
	}
	@SuppressWarnings("unchecked")
	public Collection<DifferentMea> findDifferentMeas(final Technology technology,
			final Catalog catalog) {
		return (Collection<DifferentMea>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria = session.createCriteria(DifferentMea.class);
				criteria.add(Restrictions.eq("technology",technology));
				criteria.add(Restrictions.eq("catalog",catalog));
				return criteria.list();
			}
		});
	}
}
